import {
  startService,
  grabOrders,
  reportLocation,
  haveDelivered,
  havePurchased,
  initiationOfPayments,
} from '@/api/neighbor.js';
export default {
  data() {
    return {
      buttonDisabled: false,
    };
  },
  mounted() {},
  methods: {
    initiationOfPaymentsTap(data, cb = () => {}) {
      if (!data.order_id) return;
      this.buttonDisabled = true;
      initiationOfPayments({ order_id: data.order_id, advance_payment_price: data.advance_payment_price, advance_payment_images: data.advance_payment_images  })
        .then((res) => {
          if (res.code == 200) {
            this.buttonDisabled = false;
            uni.$uv.toast('操作成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {
          this.buttonDisabled = false;
        });
    },
    havePurchasedTap(data, cb = () => {}) {
      if (!data.order_id) return;
      this.buttonDisabled = true;
      havePurchased(data)
        .then((res) => {
          if (res.code == 200) {
            this.buttonDisabled = false;
            uni.$uv.toast('操作成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {
          this.buttonDisabled = false;
        });
    },
    haveDeliveredTap(data, cb = () => {}) {
      if (!data.order_id) return;
      this.buttonDisabled = true;
      haveDelivered(data)
        .then((res) => {
          if (res.code == 200) {
            this.buttonDisabled = false;
            uni.$uv.toast('操作成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {
          this.buttonDisabled = false;
        });
    },
    startServiceTap(order_id, cb = () => {}) {
      if (!order_id) return;
      this.buttonDisabled = true;
      startService({ order_id })
        .then((res) => {
          if (res.code == 200) {
            this.buttonDisabled = false;
            uni.$uv.toast('操作成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {
          this.buttonDisabled = false;
        });
    },
    reportLocationTap(order_id, cb = () => {}) {
      if (!order_id) return;
      reportLocation({ order_id })
        .then((res) => {
          if (res.code == 200) {
            uni.$uv.toast('上报成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {});
    },
    grabOrdersTap(order_id, cb = () => {}) {
      if (!order_id) return;
      uni.showLoading({ title: '抢单中', mask: true });
      grabOrders({ order_id })
        .then((res) => {
          if (res.code === 200) {
            uni.hideLoading();
            uni.$uv.toast('抢单成功');
            setTimeout(() => {
              cb(true);
            }, 500);
          }
        })
        .catch(() => {
          uni.hideLoading();
        });
    },
  },
};
